﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SBACN.WebSite.Cache;
using System.Linq.Expressions;
using SBACN.WebSite.Models;
using SBACN.WebSite.Code;

namespace SBACN.WebSite.Admin.Feedback
{
	public partial class ComplainDetail : BasePage
	{
		public string SystemType
		{
			get
			{
				if (ViewState["SystemType"] == null)
					return this.SystemCode;
				else
					return ViewState["SystemType"].ToString();
			}
			private set
			{
				ViewState["SystemType"] = value;
			}
		}

		public string PTitle { get; set; }
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!AdminBasePage.CheckCodeRight(AdminBasePage.AdminInfo, SystemCode))
			{
				AdminBasePage.RedirectWithMessage(AdminDefaultPage, "您没有访问当前页面的权限,请与系统管理员联系!");
				return;
			}

			if (!Page.IsPostBack)
			{
				if (!string.IsNullOrEmpty(Request.QueryString["t"]) && Request.QueryString["t"] == "feedback")
				{
					tr_tel.Visible = tr_type.Visible = false;
					this.PTitle = "内容编辑：会员留言";
				}
				else
				{
					exDataBind(this.dpl_Type, SystemMapCache.Instance.List.Where(p => p.Code.StartsWith(SystemType) && p.Code != SystemType), "Code", "Name");
					this.PTitle = "内容编辑：投诉信箱 ";
				}
				exDataBind(this.radl_Status, FeedbackCache.StatusDictionary, "Key", "Value");
				this.radl_Status.Attributes.Add("class", "form-body-1");
				BindData();
			}
		}

		private void BindData()
		{
			string id = Request.QueryString["id"];
			if (!string.IsNullOrEmpty(id))
			{
				int i = int.Parse(id);

				Expression<Func<TB_Feedback, bool>> f = (p => p.ID == i);
				var r = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_Feedback>(f);
				if (r != null)
				{
					lblContent.Text = r.Content;
					txt_Content.Text = r.Memo;
					dpl_Type.SelectedValue = r.Type.ToString();
					radl_Status.SelectedValue = r.Status.ToString();
					if (!string.IsNullOrEmpty(Request.QueryString["t"]) && Request.QueryString["t"] == "feedback")
					{
						Expression<Func<TB_User, bool>> ff = (p => p.UserName == r.UserIdentity);
						var rr = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_User>(ff);
						if (rr != null)
						{
							lblUser.Text = rr.UserName;
							lblEmail.Text = rr.EMail;
						}
					}
					else
					{
						Expression<Func<TB_PublicUser, bool>> ff = (p => p.EMail == r.UserIdentity);
						var rr = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_PublicUser>(ff);
						if (rr != null)
						{
							lblUser.Text = rr.Name;
							lblTel.Text = rr.Telephone;
							lblEmail.Text = r.UserIdentity;
						}
					}
				}
			}
		}
		private void exDataBind(ListControl control, object dataSource, string valueField, string textField)
		{
			control.DataSource = dataSource;
			control.DataValueField = valueField;
			control.DataTextField = textField;
			control.DataBind();
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			int id = int.Parse(Request.QueryString["id"]);
			Expression<Func<TB_Feedback, bool>> ff = (p => p.ID == id);
			var resultR = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_Feedback>(ff);
			if (resultR != null)
			{
				if (string.IsNullOrEmpty(Request.QueryString["t"]))
				{
					resultR.Type = dpl_Type.SelectedValue;
				}
				resultR.Status = int.Parse(radl_Status.SelectedValue);
				resultR.Memo = txt_Content.Text;
				DAL.DALHelper.Instance.Update<TB_Feedback>(resultR);
				DAL.DALHelper.Instance.SaveChanges();
				RefreshNewsList();
			}

		}

		private void RefreshNewsList()
		{
			string clientJs = "<script type=\"text/javascript\" language=\"javascript\">";
			clientJs += "if(window.opener && window.opener.RefreshNewsList) window.opener.RefreshNewsList();";
			clientJs += "window.close();</script>";
			Response.Write(clientJs);
			Response.End();
		}

	}
}